<template>
    <div class="container">
        <headerView :headerTitle="headerTitle" style="background-color: #d9ebec;" />
        <div class="back-icon">
            <div class="left-icon" style="color: #555;"><el-icon @click="router.go(-1)">
                    <ArrowLeftBold />
                </el-icon></div>
            <div class="right-icon" style="color: #22B130;font-size: 25px;" @click="submitUpload">
                <el-icon>
                    <Promotion />
                </el-icon>
            </div>
        </div>
        <div class="content">
            <div class="describe">
                <p>上传描述:</p>
                <el-input v-model="describe" maxlength="200" show-word-limit style="width: 100%;font-size: 12px;"
                    :autosize="{ minRows: 5 }" type="textarea" placeholder="最多200个字..." />
            </div>
            <div class="upload-item">
                <label class="input-button" for="upload"> 选择文件 </label>
                <div class="tip">
                    <p>同时最多可以上传10个文件</p>
                    <p>{{ filesName.length }}/10</p>
                </div>
                <input type="file" id="upload" multiple="true" :fileList @change="fileChange($event)" />
                <div class="file-list-container">
                    <div class="file-list" v-if="showFileList" v-for="fileName in filesName" :key="fileName">
                        <p style="color: #999;max-width: 260px;">{{ fileName }}</p>
                        <p class="row-icon" @click="removeFile(fileName, $event)"><el-icon>
                                <CircleClose />
                            </el-icon>
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup name="Upload">
import { ref } from 'vue';
import service from '@/utils/request';
import { useRoute, useRouter } from 'vue-router';
import headerView from '@/components/Header/index.vue';
const headerTitle = ref({
    title: '上传文件',
    aline: 'center'
});
const route = useRoute();
const router = useRouter();
const parentId = ref(route.params.parentId);
//用于选择上传文件
const fileList = ref(null);
// 用于像后端发送数据用
const uploadFiles = ref([]);
const filesName = ref([]);
const describe = ref('');
const showFileList = ref(false);
const fileChange = (event) => {
    showFileList.value = true;
    fileList.value = event.target.files;
    if (filesName.value.length < 10) {
        for (let i = 0; i < fileList.value.length; i++) {
            // fileList 去重
            if (!filesName.value.includes(fileList.value[i].name)) {
                if (filesName.value.length < 10) {
                    filesName.value.push(fileList.value[i].name);
                    uploadFiles.value.push(fileList.value[i]);
                } else {
                    ElMessage({
                        message: '超出最大文件数',
                        type: 'error',
                        plain: true,
                    })
                    return;
                }
            }
        }
    } else {
        ElMessage({
            message: '超出最大文件数',
            type: 'error',
            plain: true,
        })
        return;
    }
};
const removeFile = (fileName, event) => {
    event.stopPropagation();
    filesName.value = filesName.value.filter((name) => name !== fileName);
    uploadFiles.value = uploadFiles.value.filter((file) => file.name !== fileName);
    if (filesName.value.length === 0) {
        showFileList.value = false;
    }
    console.log(uploadFiles.value.length);
};
const submitUpload = () => {
    const formData = new FormData();
    uploadFiles.value.forEach((file) => {
        formData.append('files', file);
    })
    formData.append('Describe', describe.value);
    formData.append('FileDirectoryId', parentId.value);
    service({
        url: 'api/FileInfos/upload',
        method: 'post',
        data: formData,
    }).then(res => {
        if (res != undefined) {
            if (res.status === 200) {
                router.go(-1);
                ElMessage({
                    message: '上传成功',
                    type: 'success',
                    plain: true,
                });
            }
        }
    }).catch(err => {
        router.go(-1);
        ElMessage({
            message: '上传失败',
            type: 'error',
            plain: true,
        });
    });
};
</script>

<style scoped>
.container {
    widows: 100vw;
    height: 100vh;
    background-color: #d9ebec;
}

.back-icon {
    width: 100%;
    height: 40px;
    line-height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.left-icon,
.right-icon {
    width: 60px;
    height: 40px;
    line-height: 40px;
    text-align: center;
}

.content {
    width: 80%;
    height: auto;
    margin: 0 auto;
}

.upload-item {
    width: 100%;
    min-height: 100px;
    height: auto;
}

.tip {
    margin: 10px;
    width: 100%;
    height: 20px;
    line-height: 20px;
    font-size: 12px;
    color: #999;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.input-button {
    padding: 6px 25px;
    background: #00bfff;
    border-radius: 4px;
    color: white;
    cursor: pointer;
}

input {
    display: none;
}

.describe {
    width: 100%;
    height: auto;
    margin-bottom: 25px;
}

.file-list-container {
    margin: 10px 0;
}

.file-list {
    width: 100%;
    height: 25px;
    line-height: 25px;
    display: flex;
    justify-content: space-between;
    align-items: start;
    background-color: rgb(215 230 231);
    border-bottom: 1px solid #ccc;
    padding-left: 10px;
}

.file-list p {
    height: auto;
    line-height: 25px;
    font-size: 12px;
    font-weight: 500;
    /* 单行显示，超出部分省略号显示 */
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.row-icon {
    color: #f15959;
    font-size: 18px;
    margin-right: 10px;
}
</style>